package org.black.phoneUtility.sms.schedule.app.fragment;

import java.util.Calendar;

import org.black.phoneUtility.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TimePicker;

public class SmsScheduleDateTimeTimePickerDialogFragment extends DialogFragment {

	public static final String TAG = SmsScheduleDateTimeTimePickerDialogFragment.class
			.getName();
	public static final String ARGUMENTS_HOUR_OF_DAY = "hourOfDay";
	public static final String ARGUMENTS_MINUTE = "minute";

	public interface SmsScheduleDateTimeTimePickerDialogFragmentInterface {
		public void onSmsScheduleDateTimeTimePickerDialogPositiveClick(
				int hourOfDay, int minute);

		public void onSmsScheduleDateTimeTimePickerDialogNegativeClick();
	}

	private SmsScheduleDateTimeTimePickerDialogFragmentInterface smsScheduleDateTimeTimePickerDialogFragmentInterface;

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try {
			smsScheduleDateTimeTimePickerDialogFragmentInterface = (SmsScheduleDateTimeTimePickerDialogFragmentInterface) activity;
		} catch (ClassCastException e) {
			throw new ClassCastException(
					activity.toString()
							+ " must implement SmsScheduleDateTimeTimePickerDialogFragmentInterface");
		}
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		super.onCreateDialog(savedInstanceState);
		Bundle bundle = getArguments();
		int hourOfDay = bundle.getInt(ARGUMENTS_HOUR_OF_DAY);
		int minute = bundle.getInt(ARGUMENTS_MINUTE);
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

		LayoutInflater inflater = getActivity().getLayoutInflater();
		View view = inflater.inflate(
				R.layout.sms_schedule_schedule_on_time_picker_dialog, null);
		final TimePicker smsScheduleDatePicker = (TimePicker) view
				.findViewById(R.id.smsScheduleScheduleOnTimePicker);
		boolean is24HourView = DateFormat.is24HourFormat(getActivity());
		smsScheduleDatePicker.setIs24HourView(is24HourView);
		smsScheduleDatePicker.setCurrentHour(hourOfDay);
		smsScheduleDatePicker.setCurrentMinute(minute);

		Button oneHourButton = (Button) view
				.findViewById(R.id.smsScheduleScheduleOnTime1HourButton);
		oneHourButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Calendar calendar = Calendar.getInstance();
				calendar.add(Calendar.HOUR_OF_DAY, 1);
				int hourOfDay = calendar.get(Calendar.HOUR_OF_DAY);
				int minute = calendar.get(Calendar.MINUTE);
				smsScheduleDatePicker.setCurrentHour(hourOfDay);
				smsScheduleDatePicker.setCurrentMinute(minute);
			}
		});

		builder.setView(view)
				.setPositiveButton(android.R.string.ok,
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								smsScheduleDateTimeTimePickerDialogFragmentInterface
										.onSmsScheduleDateTimeTimePickerDialogPositiveClick(
												smsScheduleDatePicker
														.getCurrentHour(),
												smsScheduleDatePicker
														.getCurrentMinute());

							}
						})
				.setNegativeButton(android.R.string.cancel,
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {

							}
						});
		return builder.create();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		smsScheduleDateTimeTimePickerDialogFragmentInterface = null;
	}

}
